﻿using System.Data;

namespace _20EFCore乐观并发控制;

class Program
{
    static void Main(string[] args)
    {
        MyDbContext ctx = new MyDbContext();
        House h = ctx.Houses.First(h => h.Id == 1);
        Console.WriteLine("输入名称");
        string name = Console.ReadLine();
        if (!string.IsNullOrEmpty(h.Owner))
        {
            if (h.Owner == name)
            {
                Console.WriteLine("房子本来就是你的");
            }
            else
            {
                Console.WriteLine($"房子已经是{h.Owner}的了");
            }
            Console.ReadLine();
            return;
        }

        h.Owner = name;

        Thread.Sleep(5000);
        try
        {
            ctx.SaveChanges();
            Console.WriteLine("恭喜你抢到了");
            Console.ReadLine();
        }
        catch (DBConcurrencyException e)
        {
            Console.WriteLine("没抢到！" + e.Message);
            Console.ReadLine();
        }
    }
}